<?php

namespace app\admin\logic;

use app\common\model\mysql\AccountModel;
use app\common\model\mysql\GroupModel;
use app\common\logic\BaseLogic;
use app\common\facade\encrypt\Password;
use app\common\model\mysql\ProjectModel;

class AccountLogic extends BaseLogic
{
    public function index($request)
    {

        $search = $request->param();
        $where = [['id', '<>', 1]];
        if (!empty($search['name'])) {
            $where[] = ['name', 'like', "%{$search['name']}%"];
        }
        if (!empty($search['mobile'])) {
            $where[] = ['mobile', '=', $search['mobile']];
        }
        $list = AccountModel::getPaginate($where, '*', 50, ['id' => 'DESC'], $search);
        // 获取组信息
        $group = GroupModel::getList(['status' => '1'], 'id,name');

        if (!empty($group)) {
            $group = $group->toArray();
            $group = array_column($group, 'name', 'id');
        }

        $project = ProjectModel::getList([], 'id,name');


        return view('Account/account', compact('list', 'search', 'group', 'project'));
    }

    public function create($request)
    {
        // 获取上级组数据
        $group_list = GroupModel::getList(['status' => 1], ['id', 'name', 'parent_id', 'status'], [], ['id' => 'ASC']);

        if (!empty($group_list[0])) {
            $group_list = getTree(objToArray($group_list));
            $list = arrayToObj(structureTree($group_list));
        }
        $project = ProjectModel::getList([], ['id', 'name']);

        return view('Account/account_add', compact('list', 'project'));
    }

    public function save($request)
    {
        $param = $request->param();

        $inputData = [
            'name' => $param['name'],
            'password' => Password::encryptPassword($param['password']),
            'realname' => $param['realname'],
            'mobile' => $param['mobile'],
            'status' => $param['status'],
            'group_ids' => in_array(1, $param['group_ids']) ? 1 : implode(',', $param['group_ids']),
            'project_ids' => empty($param['project_ids']) ? '' : implode(',', $param['project_ids']),
        ];

        if ($request->has('headimg.name', 'file', true)) {
            $file = request()->file('headimg');
            // 上传到本地服务器
            $inputData['headimg'] = \think\facade\Filesystem::disk('public')->putFile('headimg', $file);
        }

        try {
            AccountModel::addData($inputData);
        } catch (\Exception $e) {
            return $this->responseError('保存失败');
        }
        return $this->responseSuccess('保存成功');
    }

    public function edit($request, $id)
    {
        // 获取上级组数据
        $group_list = GroupModel::getList(['status' => '1'], ['id', 'name', 'parent_id', 'status'], [], ['id' => 'ASC']);
        if (!empty($group_list[0])) {
            $group_list = getTree(objToArray($group_list));
            $list = arrayToObj(structureTree($group_list));
        }

        // 获取账号信息
        $info = AccountModel::getOne(['id' => $id]);

        $project = ProjectModel::getList([], ['id', 'name']);

        return view('Account/account_edit', compact('list', 'info', 'project'));
    }

    public function update($request, $id)
    {
        $param = $request->param();

        $inputData = [
            'name' => $param['name'],
            'realname' => $param['realname'],
            'mobile' => $param['mobile'],
            'status' => $param['status'],
            'group_ids' => in_array(1, $param['group_ids']) ? 1 : implode(',', $param['group_ids']),
            'project_ids' => empty($param['project_ids']) ? '' : implode(',', $param['project_ids']),
        ];

        if ($request->has('headimg.name', 'file', true)) {
            $file = request()->file('headimg');
            // 上传到本地服务器
            $inputData['headimg'] = \think\facade\Filesystem::disk('public')->putFile('headimg', $file);
        }
        !empty($param['password']) ? $inputData['password'] = Password::encryptPassword($param['password']) : false;

        try {
            AccountModel::editData($inputData, ['id' => $id]);
        } catch (\Exception $e) {
            return $this->responseError('保存失败');
        }

        return $this->responseSuccess('保存成功');
    }
}